package org.jxht.web.api;

import org.jxht.dto.StudentDto;
import org.jxht.service.StudentService;
import org.jxht.util.GsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by huang on 2016/11/21 0021.
 */
@Controller
@RequestMapping("JxhtWeb/StudentController")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 添加学生信息
     * @param requestId 执行该功能的用户Id
     * @param requestJson Json字符串
     * @return String
     */

    @RequestMapping(value = "/{requestId}/addStudentInfo",method = RequestMethod.POST)
    public String addStudentInfo(@PathVariable(value = "requestId")int requestId,
                                  @RequestParam(value = "requestJson")String requestJson){
        List<StudentDto> dtos = GsonUtil.parseJsonArrayWithGson(requestJson,StudentDto.class);
        for(StudentDto dto:dtos){
            studentService.insertStudentService(dto);
        }
        return "SUCCESS";
    }

    /**
     * 更新学生信息
     * @param requestId 执行该功能的用户Id
     * @param requestJson Json字符串
     * @return
     */
    @RequestMapping(value = "/{requestId}/changeStudentInfo",method = RequestMethod.POST)
    public String changeStudentInfo(@PathVariable(value = "requestId")int requestId,
                                    @RequestParam(value = "requestJson")String requestJson){
        return "SUCCESS";
    }

    @RequestMapping(value = "/{requestId}/deleteStudentInfo")
    public String deleteStudentInfo(@PathVariable(value = "requestId")int requestId){
        studentService.deleteStudentService(new StudentDto(requestId));
        return "SUCCESS";
    }

    @RequestMapping(value = "/{requestId}/GetStudentInfo")
    @ResponseBody
    public List getStudentInfo(@PathVariable(value = "requestId")int requestId,
                               @RequestParam(value = "requestWay")String requestWay){
        return studentService.queryStudentService(requestId,requestWay);
    }

    @RequestMapping(value = "/{requestId}/getStudentInfoSiglne")
    @ResponseBody
    public StudentDto getStudentInfoSigln(@PathVariable(value = "requestId")int requestId){
        StudentDto dto = new StudentDto(requestId);
        return studentService.queryStudentServiceSingle(dto);
    }
}
